<?php

class FeaturedModule extends GigaModule
{
	var $name = 'Featured Module';
	
	var $configuration = array(
		'display_time' => array(
			'type' => 'text', 
			'label' => 'Display Time (in Seconds)', 
			'group' => 'add',
			'default' => '5',
		),
		'width' => array(
				'type' => 'text',
				'label' => 'Width (in pixels)',
				'group' => 'add',
				'default' => '360',
		),
		'height' => array(
				'type' => 'text',
				'label' => 'Height (in pixels)',
				'default' => '240',
				'group' => 'add',
		),
		'featured_listings' => array('type' => 'parent',
			'childInfo' => array(
					'model' => 'ModuleFeatured',
					'child_group' => 'add',
					'allowMultiple' => true
			),
			'ignoreCallback' => true,
			'label'=>"Featured Listings",
			'addMoreLabel'=>'Add more featured listings',
			'group' => 'add',
		)
	);
	
	function __construct(){
		$this->pageProperties['after'] = 
				"<script type = 'text/javascript'> 
				$('.featured_module_category').live('beforeFetchingOptions',function(){	
				$(this).data('dynamicOptionUrl','/cms/getOptions/ModuleFeatured.category_id/'); 	
				});				
				$('#featured_listings_parent_field_add_more').live('onLoad', function(){ $(this).text('Add more featured listings'); });
				$('#Module.featured_listings_parent_field_add_more').live('onLoad', function(){ $(this).text('Add more featured listings'); });
				</script>";
		$this->configuration['display_time']['label']= __('Display Time (in Seconds)');
		$this->configuration['featured_listings']['label']= __('Featured Listings');
		$this->configuration['featured_listings']['addMoreLabel']= __('Add more featured listings');
		
		$validationOptions = array(
				'notEmpty'=>array(
						'rule' => VALID_NOT_EMPTY,
						'message' => 'This field is required',
						'required' => true,
						'allowEmpty' => false
				),
				'notInteger'=>array(
						'rule' => '/^\d+$/',
						'message' => 'Invalid integer value',
						'required' => false,
						'allowEmpty' => true
				),
				'notZero'=>array(
						'rule' => array('comparison','>',0),
						'message' => 'Entered value should be greater than 0',
						'required' => false,
						'allowEmpty' => true
				)
		);
		$this->configuration['display_time']['validate'] = $validationOptions;
		$this->configuration['width']['validate'] = $validationOptions;
		$this->configuration['height']['validate'] = $validationOptions;
	}
	
	function beforeSave($_data){
		return $this->_processListingImage($_data);
	}
	
	function render($params)
	{
		$hasImages = false;
		$imagesRecord = array();
		if(!empty($params['featured_listings']))
		{
			//to hide those records which do not have images
			foreach ($params['featured_listings'] as $key=>$record)
			{
				if(isset($record['listing_image']))
				{
					if(file_exists(WWW_ROOT .DS. "uploads" .DS. "featured_listing_images" .DS. $record['listing_image']))
					{
						$hasImages = true;
						$imagesRecord[] = $record;
					}
				}
			}
			$params['featured_listings'] = $imagesRecord;
		}
		if($hasImages)
		{
			return ClassRegistry::getObject("View")->element('featured_listings',array('params'=>$params));
		}
		return false;
	}
	
	function _processListingImage($_data)
	{
		$ListingModelObj = cri('Listing');
		if(!empty($_data) && isset($_data['Module']))
		{
			if(isset($_data['Module']['featured_listings']) && is_array($_data['Module']['featured_listings']))
			{
				$existingModuleData = null;
				if(isset($_data['Module']['id']) && $_data['Module']['id'])
				{
					$existingModuleData = cri("Modules.Module")->findById($_data['Module']['id']);
				}
				$destinationDirPath = WWW_ROOT.DS.'uploads'.DS.'featured_listing_images';
				if(!file_exists($destinationDirPath))
				{
					@mkdir($destinationDirPath);
				}
				$ListingModelObj->recursive = -1;
				foreach ($_data['Module']['featured_listings'] as $key=>$record)
				{
					if(isset($_data['Module']['featured_listings'][$key]['listing_id']) && @$_data['Module']['featured_listings'][$key]['listing_id'])
					{
						$tmp = $ListingModelObj->findById($_data['Module']['featured_listings'][$key]['listing_id']);
						$_data['Module']['featured_listings'][$key]['listing_name'] = $tmp['Listing']['title'];
						$_data['Module']['featured_listings'][$key]['listing_url'] = $tmp['Listing']['viewUrl'];
					} else {
						$_data['Module']['featured_listings'][$key]['listing_name'] = "";
						$_data['Module']['featured_listings'][$key]['listing_url'] = cr("cmsConfig.Backend.siteUrl");
					}
					if(isset($record['listing_image']) && $record['listing_image']['name'] == '')
					{
						unset($_data['Module']['featured_listings'][$key]['listing_image']);
						if(!empty($existingModuleData))
						{
							if(isset($existingModuleData['Module']['params']['featured_listings'][$key]) && isset($existingModuleData['Module']['params']['featured_listings'][$key]['listing_image']) && $existingModuleData['Module']['params']['featured_listings'][$key]['listing_image'])
							{
								$_data['Module']['featured_listings'][$key]['listing_image'] = $existingModuleData['Module']['params']['featured_listings'][$key]['listing_image'];
							}
						}
					}
					elseif($record['listing_image'])
					{
						$source = $record['listing_image']['tmp_name'];						
						$name = $record['listing_image']['name'];
						$ext = substr($name, strrpos($name, "."));
						$name = substr($name, 0, strrpos($name, "."));						
						$locImage = $record['listing_id'].'_'.Inflector::slug($name).$ext;
						$destination = $destinationDirPath.DS.$locImage;
						if(move_uploaded_file($source, $destination))
						{
							unset($_data['Module']['featured_listings'][$key]['listing_image']);
							$_data['Module']['featured_listings'][$key]['listing_image'] = $locImage;
							//$_data['Module']['featured_listings'][$key]['image_name'] = $locImage;
						}
					}
				}
			}
		}
		unset($ListingModelObj);
		return $_data;
	}
	
}
?>
